<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20220315
 * Time: 11:05 
 * 账户管理
 */
namespace app\user\controller;

use  Mscore\Core\Controller;
use  app\user\model\Common as CommonModel;
use  app\user\model\Account as AccountModel;
use  app\admin\model\Login as LoginModel;
use  app\user\model\Qrocde as QrocdeModel;
use  Mscore\Core\Lib\ArrayMethod;
use  Mscore\Core\Lang;
use  Mscore\Core\Util\Util;
use  Mscore\Core\Lib\Mailer;


class RobotController extends Controller{
    
    private $model;
    private $login;
    private $error;
    private $success;
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->model = new AccountModel;
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    /**
     * 账户列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list(){
        $post = post();
        // print_r($this->login['checkrole']);die;
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            // print_r(post('roletype'));
            $where = ' a.dels < 1 and a.type=2';
            if($this->login['info']['ranges']<1){
                $where .= ' and (a.pid = '.$this->login['info']['aid'].' or a.aid='.$this->login['info']['aid'].')';
            }
            if(post('type')){
                $where .= ' and a.grade = "'.post('type').'" ';
            }
            if(post('username')){
                $where .= ' and a.username like "%'.post('username').'%" ';
            }
            if(post('mobile')){
                $where .= ' and a.mobile like "%'.post('mobile').'%" ';
            }
            if(post('realname')){
                $where .= ' and a.realname like "%'.post('realname').'%" ';
            }
            if(post('roletype')){
                $where .= ' and a.roletype = '.post('roletype');
            }
            $datalist = $this->model->list($where,$page,$limit);
            $data = [
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            ];
            $this->echojson($data);
        } else {
            $rolelist = (new CommonModel())->getrole('dels<1 and status>0');
            $type=!empty(get('type'))?get('type'):'';
            $this->assign('type', $type);
            $this->assign('rolelist', $rolelist);
            $this->display('list.html');
        }
    }

    /**
     * 账户添加
     * @param array $add 
     * @param array $where 
     */ 
    public function add(){
       $params = post();
       if($params){
           //post请求则保存
           $data = $this->model->save($params);
           switch ($data) {
              case true:
                $this->echoJson($this->success);
                break;
                    
              default:
                $this->echoJson($this->error);
                break;
            }  
       }else{
           $rolelist = (new CommonModel())->getrole('dels<1 and status>0');
           
           if(get('id')){
               $info = $this->model->info('aid='.get('id'));
           }
           // 判断是否管理员添加
           if($this->login['info']['grade']=='superadmin'){
               $pid = isset($info['pid'])&&!empty($info['pid'])?$info['pid']:0;
           }else{
               $pid = isset($info['pid'])&&!empty($info['pid'])?$info['pid']:$this->login['info']['aid'];
           }
            $grade=isset($info)?$info['grade']:'member';
            foreach ($rolelist as $key => $value) {
                if($value['grade']==$grade){
                    $roletype=$value['id'];
                    $ranges = $value['ranges'];
                }
            }
            //定义变量info输出
            $info['aid']=isset($info['aid'])?$info['aid']:'';
            $info['username']=isset($info['username'])?$info['username']:'';
            $info['password']=isset($info['password'])&&!empty($info['password'])?'':'000000';
            $info['paypwd']='';
            $info['amount']=isset($info['amount'])?$info['amount']:2000;
            $info['realname']=isset($info['realname'])?$info['realname']:'';
            $info['mobile']=isset($info['mobile'])?$info['mobile']:'';
            $info['email']=isset($info['email'])?$info['email']:'';
            $info['roletype']=isset($roletype)?$roletype:$info['roletype'];
            $info['ranges']=isset($ranges)?$ranges:$info['ranges'];
            $info['grade']=isset($info['grade'])?$info['grade']:$grade;
            $info['pid']=isset($info['pid'])?$info['pid']:$pid;
            $info['subrealname']=isset($info['subrealname'])?$info['subrealname']:'';
            $info['header_url']=isset($info['header_url'])?$info['header_url']:'';
            $info['status']=isset($info['status'])?$info['status']:0;
            $info['usercode']=isset($info['usercode'])?$info['usercode']:'';
            // $info['credibility']=isset($info['credibility'])?$info['credibility']:420;
            // $info['borrow']=isset($info['borrow'])?$info['borrow']:0;
           
            $this->assign('user', $this->login['info']);
            $this->assign('rolelist', $rolelist);
            $this->assign('datainfo', $info);
            $this->display('add.html');
       }
    }

    /**
     * 批量添加
     * @param array $batchadd 
     * @param array $where 
     */ 
    public function batchadd(){
       $params = post();
       if($params){
            $datainfo=$this->model->robotsave($params);
            switch ($datainfo) {
              case true:
                $this->echoJson($this->success);
                break;
                    
              default:
                $this->echoJson($this->error);
                break;
            } 
       }else{
          $this->display('batchadd.html');
       }
    }
    
    /**
     * 人员删除
     * @param array $userdel 
     * @param array $where 
     */  
    public function del(){ 
        $aid = post('id');
        $user=array('aid'=>$aid);
        $datadel = $this->model->del($user);
        switch ($datadel) {
            case true:
                $this->echoJson($this->success);
                break;
                    
            default:
                $this->echoJson($this->error);
                break;
        }  
    }

    
    /**
     * 查看密码
     * @param array $showpwd 
     * @param array $where 
     */ 
    public function showpwd(){
        $post = post();
        if ($post) {
            $user = $this->model->info(' aid='.$post['aid'],'showpwd');
            $user=isset($user['showpwd'])&&!empty($user['showpwd'])?json_decode($user['showpwd'],true):['password'=>'','paypwd'=>''];
            $spwd=(new CommonModel())->getBaseinfo('keyname="authweight"', 'content')['content'];
            $spwd['use']=isset($spwd['use'])&&!empty($spwd['use'])?$spwd['use']:2;
            if( $spwd['use'] > 1 ){
                if($spwd['use']==2){
                    $id=(new CommonModel())->getcode($post['code']);
                }
                if($spwd['use']==3){
                    $id=(new CommonModel())->getcode($post['code'], ' and aid='.$post['aid']);
                }
                if(!$id){return $this->echojson($this->error);}
            }
            switch ($spwd['type']) {
                case 1:
                    $user['paypwd']=!empty($user['paypwd'])?Util::phpDecode($user['paypwd']):'1';
                    $this->success['data']=['paypwd'=>$user['paypwd']];
                    return $this->echojson($this->success);
                    break;
                case 2:
                    $user['password']=!empty($user['password'])?Util::phpDecode($user['password']):'1';
                    $this->success['data']=['password'=>$user['password']];
                    return $this->echojson($this->success);
                    break;
                case 3:
                    $user['password']=!empty($user['password'])?Util::phpDecode($user['password']):'1';
                    $user['paypwd']=!empty($user['paypwd'])?Util::phpDecode($user['paypwd']):'1';
                    $this->success['data']=['paypwd'=>$user['paypwd'],'password'=>$user['password']];
                    return $this->echojson($this->success);
                    break;
            }
            return $this->echojson($this->error);
        }else{
            if(empty(get('id'))){
                echo "参数丢失";
                exit;
            }
            $spwd=(new CommonModel())->getBaseinfo('keyname="authweight"', 'content')['content'];
            $spwd['use']=isset($spwd['use'])&&!empty($spwd['use'])?$spwd['use']:2;
            $this->assign('spwd', $spwd);
            $this->assign('aid', get('id'));
            $this->display('showpwd.html');
        }  
    }
}
